package com.zhitong.ztblog.mappers;

import com.zhitong.ztblog.entity.Blog;
import com.zhitong.ztblog.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

/**
 * 博客的API
 */
@Mapper
public interface BlogMapper {

//    根据博客id查找博客
    @Results(id = "blogMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "sid",column = "sid"),
            @Result(property = "title",column = "title"),
            @Result(property = "intro",column = "intro"),
            @Result(property = "imageUrl",column = "imageUrl"),
            @Result(property = "content",column = "content"),
            @Result(property = "createTime",column = "createTime"),
            @Result(property = "lastTime",column = "lastTime"),
            @Result(property = "totle",column = "totle"),
            @Result(property = "user",column = "uid",one = @One(select = "com.zhitong.ztblog.mappers.UserMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "sortName",column = "sid",one = @One(select = "com.zhitong.ztblog.mappers.SortMapper.findNameById",fetchType = FetchType.LAZY))

    })
    @Select("select * from blog where id = #{id}")
    public Blog findById(int id);

//    查询所有博客
    @ResultMap(value = "blogMap")
    @Select("select * from blog")
    public Blog[] findAll();

//    查询博客总数
    @Select("select count(*) from blog")
    public int findBlogsNum();

    //    根据分类id查询所属博客
    @ResultMap(value = "blogMap")
    @Select("select * from blog where sid=#{sid} limit #{currentPage},#{pageSize}")
    public Blog[] findBlogBySid(int sid,int currentPage,int pageSize);

    //    根据标签id查询所属博客
    @ResultMap(value = "blogMap")
    @Select("select * from blog where id in (select bid from blog_label where lid=#{lid}) limit #{currentPage},#{pageSize}")
    public Blog[] findBlogBylid(int lid,int currentPage,int pageSize);

    //  根据分类id查询该分类下的博客数量
    @Select("select count(*) from blog where sid =#{sid}")
    public int findBlogNumBySid(int sid);

    //分页博客
    @ResultMap(value = "blogMap")
    @Select("select * from blog limit #{currentPage},#{pageSize}")
    public Blog[] findBlogByPage(int currentPage,int pageSize);

    //查询查看数前五的博客
    @ResultMap(value = "blogMap")
    @Select("select * from blog ORDER BY totle DESC limit 5")
    public Blog[] findBlogByTop();

    //增加观看数
    @ResultMap(value = "blogMap")
    @Update("update blog set totle=totle+1 where id=#{id}")
    public void updateTotle(int id);

    //根据id删除博客
    @Delete("delete from blog where id=#{id}")
    public void delBlogById(int id);

    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    @Insert("Insert into blog values(null,#{sid},#{uid},#{title},#{intro},#{content},#{imageUrl},#{createTime},#{lastTime},#{totle})")
    public void insertBlog(Blog blog);

    @Update("update blog set sid=#{sid},intro=#{intro},content=#{content},imageUrl=#{imageUrl},lastTime=#{lastTime} where id=#{id}")
    public void updateBlog(Blog blog);

    @Select("select * from blog where title like concat('%',#{word},'%') or intro like concat('%',#{word},'%') or content like concat('%',#{word},'%') limit #{currentPage},#{pageSize}")
    public Blog[] findBlogByWord(String word,int currentPage,int pageSize);

    @Select("select count(*) from blog where title like concat('%',#{word},'%') or intro like concat('%',#{word},'%') or content like concat('%',#{word},'%')")
    public int findBlogNumsByWord(String word);
}
